<?php

class Controller_Admin_Promotions extends Controller_Admin_Template {
	
	public function before() {
		parent::before();
		
		$this->template->title = __('Promotions');
	}

	public function action_index() {
		$this->template->title = __('Promotions');
		$view = View::factory('admin/promotions/index');
		
		$current_promotions = ORM::factory('promotion')
			->where('starting_date', '<=', strtotime("now"))
			->and_where('ending_date', '>=', strtotime("now"))
			->find_all();
		$view->set('current_promotions', $current_promotions);
		$this->template->content = $view;
	}
	
	public function action_cancel() {
		$promotion_id = $this->request->param('id');	
		$promotion = ORM::factory('promotion', $promotion_id);
		$promotion->ending_date = strtotime('now') -1000;
		$promotion->save();
		$this->request->redirect('admin/promotions/');
	}
	
	public function action_new() {
		$this->template->title = __('Create a New Promotion');
		$view = View::factory('admin/promotions/new/index');
		$list = View::factory('admin/promotions/new/listitems');

		$categories = ORM::factory('category')
			->where('parent_category_id', '=', 0)
			->order_by('name', 'asc')
			->find_all();
		
		
		$first_category = ORM::factory('category')
			->order_by('name', 'asc')
			->find();
		
		$category_id = $first_category->id;
		$subcategory_id = $category_id;
		
		$values = array();
		
		$items = array();
		
		if($_POST) {
			$category = ORM::factory('category', $_POST['subcategory_id']);
			$values = $_POST;
			if($category->parent_category_id == 0) {
				$subs = $category->subcategories->find_all();
				foreach($subs as $sub):
					foreach($sub->items->find_all() as $subitem):
						$items[] = $subitem;
					endforeach;
				endforeach;
			}
			else {
				$items = $category->items->find_all();
			}
		}
		else {
			$category = ORM::factory('category')
				->where('parent_category_id', '=', '0')
				->order_by('name', 'asc')
				->find();
			$subs = $category->subcategories->find_all();
			foreach($subs as $sub):
				foreach($sub->items->find_all() as $subitem):
					$items[] = $subitem;
				endforeach;
			endforeach;
			$values['category_id'] = $category_id;
			$values['subcategory_id'] = $subcategory_id;
		}
		
		$list->set('items', $items);
		$view->set('list', $list);
		$view->set('values', $values);
		$view->set('categories', $categories);
		$this->template->content = $view;
	}

	public function action_showcategory() {
		$category_id = $this->request->param('id');
		$items = array();	
		$category = ORM::factory('category', $category_id);
		if($category->parent_category_id == 0) {
			$subs = $category->subcategories->find_all();
			foreach($subs as $sub):
				foreach($sub->items->find_all() as $subitem):
					$items[] = $subitem;
				endforeach;
			endforeach;
		}
		else {
			$items = $category->items->find_all();
		}
		
		$view = View::factory('admin/promotions/new/listitems');
		$view->set('items', $items);
		
		$this->response->body($view);
				
	}
	
	public function action_edit() {
		$items = array();
		$view = View::factory('admin/promotions/edit');
		
		if(isset($_POST['subcategory_id'])) {
			$category = ORM::factory('category', $_POST['subcategory_id']);
			$this->template->title = __('New Promotion for '.$category->name);
			$view->set('subcategory_id', $_POST['subcategory_id']);	
		}
		else if(isset($_POST['item_id'])) {
			$item = ORM::factory('item', $_POST['item_id']);
			$this->template->title = __('New Promotion for '.$item->name);
			$view->set('item_id', $_POST['item_id']);	
			/*$items[] = $item;*/
		}
		else {
			$this->template->title = __('Error');	
		}
		
		$this->template->content = $view;
	}
	
	public function action_save() {
		$items = array();	
		try {	
			if(isset($_POST['subcategory_id'])) {
				$category = ORM::factory('category', $_POST['subcategory_id']);
				if($category->parent_category_id == 0) {
					foreach($category->subcategories->find_all() as $sub):
						foreach($sub->items->find_all() as $subitem):
							$items[] = $subitem;
						endforeach;
					endforeach;
				}
				else {
					$items = $category->items->find_all();
				}
			}
			elseif(isset($_POST['item_id'])) {
				$item = ORM::factory('item', $_POST['item_id']);
				$items[] = $item;
			}
			else {
				$this->template->title = __('ERROR');	
			}
			
			$promotion = ORM::factory('promotion');
			$promotion->values($_POST);
			
			$starting_date = 
				DateTime::createFromFormat('m/d/Y', $_POST['start_date']);
			$promotion->starting_date = $starting_date->getTimestamp();
			$ending_date = 
				DateTime::createFromFormat('m/d/Y', $_POST['end_date']);
			$promotion->ending_date = $ending_date->getTimeStamp();
			
			
			$promotion->save();
			
			foreach($items as $item) {
				$promotion_item = ORM::factory('promotionitem');
				$promotion_item->promotion_id = $promotion->id;
				$promotion_item->item_id = $item->id;
				$promotion_item->save();
			}
			
			$this->request->redirect('admin/promotions');
		}
		catch(ORM_Validation_Exception $e) {
				
		}
		
	}
	
	public function action_createforcategory() {
		$category_id = $this->request->param('id');
		$view = View::factory('admin/promotions/createforcategory')
			->set('values', $_POST)
			->set('category_id', $category_id)
			->bind('errors', $errors);
		
		if($_POST) {
			$promotion = ORM::factory('promotion')
				->values($_POST);
			
			$external_values = array() +
				Arr::get($_POST, '_external', array());
			
			$view->set('external_values', $external_values);
			
			$extra = Validation::factory($external_values)
				->rule('start_date', 'date')
				->rule('start_date', 'not_empty')
				->rule('end_date', 'date')
				->rule('end_date', 'not_empty');
					
			try {
				$promotion->save($extra);
				$promotion->starting_date = strtotime($external_values['start_date']);
				$promotion->ending_date = strtotime($external_values['end_date']);
				$promotion->save();
				
				$category = ORM::factory('category', $category_id);
				if($category->parent_category_id == 0) {
					foreach($category->subcategories->find_all() as $sub):
						foreach($sub->items->find_all() as $subitem):
							$items[] = $subitem;
						endforeach;
					endforeach;
				}
				else {
					$items = $category->items->find_all();
				}
				
				foreach($items as $item) {
					$promotion_item = ORM::factory('promotionitem');
					$promotion_item->promotion_id = $promotion->id;
					$promotion_item->item_id = $item->id;
					$promotion_item->save();
				}
				
				$this->request->redirect('admin/promotions');
			}
			catch(ORM_Validation_Exception $e) {
				$errors = $e->errors('models');
			}
		}
		
		$this->template->content = $view;
	}
	
	public function action_createforitem() {
		$item_id = $this->request->param('id');
		$view = View::factory('admin/promotions/createforitem')
		->set('values', $_POST)
		->set('item_id', $item_id)
		->bind('errors', $errors);
	
		if($_POST) {
			$promotion = ORM::factory('promotion')
			->values($_POST);
				
			$external_values = array() +
			Arr::get($_POST, '_external', array());
				
			$view->set('external_values', $external_values);
				
			$extra = Validation::factory($external_values)
			->rule('start_date', 'date')
			->rule('start_date', 'not_empty')
			->rule('end_date', 'date')
			->rule('end_date', 'not_empty');
				
			try {
				$ex = $_POST['_external'];
				$promotion->save($extra);
				$promotion->starting_date = strtotime($external_values['start_date']);
				$promotion->ending_date = strtotime($external_values['end_date']);
				$promotion->save();
	
	
				$promotion_item = ORM::factory('promotionitem');
				$promotion_item->promotion_id = $promotion->id;
				$promotion_item->item_id = $item_id;
	
				$this->request->redirect('admin/promotions');
			}
			catch(ORM_Validation_Exception $e) {
				$errors = $e->errors('models');
			}
		}
	
		$this->template->content = $view;
	}
	
}